package zb.wzby;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @author wzby
 * @Date: 2022/8/10 9:28
 */
public class Eat {
    CyclicBarrier cb = new CyclicBarrier(4,() -> {
        System.out.println("开始吃饭");
    });

    void eat(){
        Random rand = new Random();
        int time = rand.nextInt(5,20);
        String str = Thread.currentThread().getName();
        System.out.printf("%s出发了, 预计%d分钟后到达 %n",str,time);
        try{
            TimeUnit.SECONDS.sleep(time);
            System.out.printf("%s已到达%n",str);
            cb.await();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Eat s = new Eat();
        new Thread(s::eat,"Jack").start();
        new Thread(s::eat,"John").start();
        new Thread(s::eat,"Bob").start();
        new Thread(s::eat,"Mary").start();
    }
}
